const cache = new Cache(config.timeout * 3)
const data = [0, 0, 0, 0, 0]

async function main(market, base, quote) {

	const url = `http://www.qbtc.com/json/depthTable.do?tradeMarket=${quote}&symbol=${base}`
	try {
		const {statusCode, body} = await Request.getAsync(url, {timeout: config.timeout, json: true})
		// console.log(url, statusCode, body)

		if (statusCode === 200 && body.result && body.result.sell && body.result.buy) {
			const sellAry = body.result.sell.sort((pre, cur) => pre.price - cur.price)
			const buyAry = body.result.buy.sort((pre, cur) => cur.price - pre.price)

			data[1] = sellAry[0].price
			data[2] = buyAry[0].price
			data[3] = sellAry[0].amount
		    data[4] = buyAry[0].amount
			cache.put('data', data)
		}
	} catch (error) {
		LOG([market, error.code || error.message || error])
	}
	
	await Promise.delay(config.timeout * 0.5)
	main(market, base, quote)
}

exports.init = main
exports.data = cache
